<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace hdphp\services\wechat\orderShipping;

use hdphp\exceptions\AdminException;
use hdphp\services\wechat\config\MiniProgramConfig;
use EasyWeChat\Kernel\BaseClient as EasyWeChatBaseClient;

/**
 * Class：BaseClient
 * 创建人：Jack
 * 创建时间：2023/11/30 14:35
 * @package hdphp\services\wechat\orderShipping
 */
class BaseClient extends EasyWeChatBaseClient
{

    public $config;

    public $accessToken;

    const BASE_API = 'https://api.weixin.qq.com/';

    const ORDER = 'wxa/sec/order/';

    const EXPRESS = 'cgi-bin/express/delivery/open_msg/';

    const PATH = '/pages/goods/order_details/index';


    /**
     * 注释：resultHandle
     * 创建人：Jack
     * 创建时间：2023/11/30 14:35
     * @param $result
     * @return mixed
     */
    private function resultHandle($result)
    {
        if (empty($result)) {
            throw new AdminException('微信接口返回异常!');
        }
        if ($result['errcode'] == 0) {
            return $result;
        } else {
            throw  new AdminException("微信接口异常：code = {$result['errcode']} msg = {$result['errmsg']}");
        }
    }

    /**
     * 注释：发货
     * 创建人：Jack
     * 创建时间：2023/11/30 14:49
     * @param $params
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function shipping($params)
    {
        return $this->resultHandle($this->httpPostJson(self::ORDER . 'upload_shipping_info', $params));
    }

    /**
     * 注释：合单
     * 创建人：Jack
     * 创建时间：2023/11/30 14:52
     * @param $params
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function combinedShipping($params)
    {
        return $this->resultHandle($this->httpPostJson(self::ORDER . 'upload_combined_shipping_info', $params));
    }

    /**
     * 注释：签收消息提醒
     * 创建人：Jack
     * 创建时间：2023/11/30 14:52
     * @param $params
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function notifyConfirm($params)
    {
        return $this->resultHandle($this->httpPostJson(self::ORDER . 'notify_confirm_receive', $params));
    }

    /**
     * 注释：查询小程序是否已开通发货信息管理服务
     * 创建人：Jack
     * 创建时间：2023/11/30 14:52
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function isManaged()
    {
        /** @var MiniProgramConfig $make */
        $make = app()->make(MiniProgramConfig::class);
        $params = [
            'appid' => $make->get('appId')
        ];
        return $this->resultHandle($this->httpPostJson(self::ORDER . 'is_trade_managed', $params));
    }

    /**
     * 注释：设置跳转连接
     * 创建人：Jack
     * 创建时间：2023/11/30 14:52
     * @param $path
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function setMesJumpPath($path)
    {
        $params = [
            'path' => $path
        ];
        return $this->resultHandle($this->httpPostJson(self::ORDER . 'set_msg_jump_path', $params));
    }

    /**
     * 注释：获取运力id列表get_delivery_list
     * 创建人：Jack
     * 创建时间：2023/11/30 14:53
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getDeliveryList()
    {
        return $this->resultHandle($this->httpPostJson(self::EXPRESS . 'get_delivery_list', []));
    }
}
